package org.groovymud.shell.security.loginsteps;

import java.io.IOException;

import javax.security.auth.callback.Callback;
import javax.security.auth.callback.TextOutputCallback;
import javax.security.auth.callback.UnsupportedCallbackException;
import javax.security.auth.login.FailedLoginException;

import org.groovymud.shell.security.MudLoginModuleData;
import org.groovymud.shell.security.callbacks.SelfHandlingTextOutputCallback;

import static org.groovymud.shell.security.loginsteps.CallBackDelegate.*;

public class ValidateUsername extends LoginStep{

    @Override
	public void execute(MudLoginModuleData data) throws IOException, UnsupportedCallbackException, FailedLoginException {
		if(!validateUsername(data.getUsername())){
			TextOutputCallback toc = new SelfHandlingTextOutputCallback(TextOutputCallback.WARNING, "\nSorry, you cannot use that name. Try again.");
			Callback[] cb2 = createCallbackArray(toc);
			data.getCallbackHandler().handle(cb2);
			getPreviousStep().execute(data);
			return;
		}
		getNextStep().execute(data);
	}
	public boolean validateUsername(String username) {
		return !username.trim().equals("");
	}
}
